package com.example.mobile2021606176mhq0408;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private CheckBox checkBoxMainYingqing;
    private CheckBox checkBoxMainBoli;
    private CheckBox checkBoxMainFangxiangpan;
    private Button btnMainBuy;
    private TextView tvMainTip;
    private TextView tvMainResult;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        checkBoxMainYingqing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainResult.setText("选中:" + buttonView.getText().toString());
                } else {
                    tvMainResult.setText("取消选中:" + buttonView.getText().toString());
                }
            }
        });
        checkBoxMainBoli.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainResult.setText("选中:" + buttonView.getText().toString());
                } else {
                    tvMainResult.setText("取消选中:" + buttonView.getText().toString());
                }
            }
        });
        checkBoxMainFangxiangpan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    tvMainResult.setText("选中:" + buttonView.getText().toString());
                } else {
                    tvMainResult.setText("取消选中:" + buttonView.getText().toString());
                }
            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String msg = "选中:";

                if (checkBoxMainYingqing.isChecked()) {
                    Toast.makeText(getApplicationContext(), "已购买" + checkBoxMainYingqing.getText().toString(), Toast.LENGTH_SHORT).show();
                    msg += "\n" + checkBoxMainYingqing.getText().toString();
                }
                if (checkBoxMainBoli.isChecked()) {
                    Toast.makeText(getApplicationContext(), "已购买" + checkBoxMainBoli.getText().toString(), Toast.LENGTH_SHORT).show();
                    msg += "\n" + checkBoxMainBoli.getText().toString();
                }
                if (checkBoxMainFangxiangpan.isChecked()) {
                    Toast.makeText(getApplicationContext(), "已购买" + checkBoxMainFangxiangpan.getText().toString(), Toast.LENGTH_SHORT).show();

                    msg += "\n" + checkBoxMainFangxiangpan.getText().toString();
                }
                tvMainResult.setText(msg);
            }
        });
}

        private void initView() {
            tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
            checkBoxMainYingqing = (CheckBox) findViewById(R.id.checkBox_main_yingqing);
            checkBoxMainBoli = (CheckBox) findViewById(R.id.checkBox_main_boli);
            checkBoxMainFangxiangpan = (CheckBox) findViewById(R.id.checkBox_main_fangxiangpan);
            btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
            tvMainTip = (TextView) findViewById(R.id.tv_main_tip);
            tvMainResult = (TextView) findViewById(R.id.tv_main_result);
        }
}